import CommentTable.CommentTable;
import PageClassTable.PageClassTable;
import PageTable.PageTable;
import UserTable.UserTable;
import comment.Comment;
import page.Page;
import page.PageClass;
import user.User;
import utils.JdbcUtils;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.List;
import java.util.Map;

public class Main_page {
    JFrame frame = new JFrame("主页");
    String sql_user="SELECT * FROM user WHERE user_name = ?";
    public void init(String name)throws Exception{
        //设置窗体关闭模式
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置窗体大小
        frame.setSize(800, 600);
        //设置窗体居中
        frame.setLocationRelativeTo(null);
        //设置窗口不能改变大小
        frame.setResizable(false);
        //获取用户信息
        List<Map<String, Object>> user= JdbcUtils.JdbcQuery(sql_user,name);
        int id = 0;
        int power = 0;
        String password = null;
        for(Map<String,Object> map : user){
            for(String key : map.keySet()){
                if(key.equals("user_password")) password= (String) map.get(key);
                if(key.equals("user_id")) id= (int) map.get(key);
                if(key.equals("user_power")) power= (int) map.get(key);
            }
        }
        //获取用户信息
        JSplitPane sp=new JSplitPane();
        sp.setContinuousLayout(true);
        sp.setDividerLocation(150);
        sp.setDividerSize(7);
        //左侧树
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("系统管理");
        DefaultMutableTreeNode user_manage = new DefaultMutableTreeNode("用户管理");
        DefaultMutableTreeNode page_manage = new DefaultMutableTreeNode("文章管理");
        DefaultMutableTreeNode page_class_manage = new DefaultMutableTreeNode("分类管理");
        DefaultMutableTreeNode comment_manage = new DefaultMutableTreeNode("评论管理");
        DefaultMutableTreeNode change_user = new DefaultMutableTreeNode("切换账户");
        root.add(user_manage);
        root.add(page_manage);
        root.add(page_class_manage);
        root.add(comment_manage);
        root.add(change_user);
        JTree tree = new JTree(root);
        tree.setSelectionRow(0);
        int finalPower = power;
        //点击条目的时候 会执行
        tree.addTreeSelectionListener(e -> {
            //得到当前选中的结点对象
            Object lastpath =  e.getNewLeadSelectionPath().getLastPathComponent();
            if(user_manage.equals(lastpath)){
                User.idChange();
                sp.setRightComponent(new UserTable(finalPower,name));
                sp.setDividerLocation(150);
            }
            if(page_manage.equals(lastpath)){
                Page.idChange();
                sp.setRightComponent(new PageTable(finalPower,name));
                sp.setDividerLocation(150);
            }
            if(page_class_manage.equals(lastpath)){
                PageClass.idChange();
                sp.setRightComponent(new PageClassTable(finalPower,name));
                sp.setDividerLocation(150);
            }
            if(comment_manage.equals(lastpath)){
                Comment.idChange();
                sp.setRightComponent(new CommentTable(finalPower,name));
                sp.setDividerLocation(150);
            }
            if(change_user.equals(lastpath)){
                int userOption =  JOptionPane.showConfirmDialog(frame,"确认退出?","退出", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                if (userOption == JOptionPane.OK_OPTION) {
                    try {
                        frame.dispose();
                        new Blog_windows().init();
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });

        sp.setLeftComponent(tree);
        frame.add(sp);
        //设置窗体是否可见
        frame.setVisible(true);
    }
}
